﻿/*********************************************************************************************************************
Unit tests VanguardDocumentation, View Level1Entities

© 2014 Erik Ackerholm

This file is part of SqlVanguard.

SqlVanguard is free software: you can redistribute it and/or modify it under the terms of the GNU General Public 
License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later 
version.

SqlVanguard is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied 
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with SqlVanguard. If not, see 
<http://www.gnu.org/licenses/>.
*********************************************************************************************************************/

set nocount on;
set xact_abort off;
GO



-- Test of existence
declare @ExpectedResult as table (
	Level0Type        varchar(64) not null,
	Level0Name        nvarchar(128) not null,
	Level1Type        varchar(64) not null,
	Level1Name        nvarchar(128) not null
);

insert into @ExpectedResult
values
	('SCHEMA', 'TestSchema', 'TABLE', 'TestTable'),
	('FILEGROUP', 'TESTFILES', 'LOGICAL FILE NAME', 'VdUnitTest_Test');

-- Unexpected rows
if
	exists (
		select Level0Type, Level0Name, Level1Type, Level1Name
		from VanguardDocumentation.Level1Entities where Level0Name like 'Test%'
		except
		select * from @ExpectedResult
	)
begin
	raiserror ('Too many results in test', 16, 1);
end;

-- Missing rows
if
	exists (
		select * from @ExpectedResult
		except
		select Level0Type, Level0Name, Level1Type, Level1Name
		from VanguardDocumentation.Level1Entities where Level0Name like 'Test%'
	)
begin
	raiserror ('Too few results in test', 16, 2);
end;
GO



-- Test of ordering
declare @TypeOrder as table (
	Level0Type        varchar(64) not null,
	Level1Type        varchar(64) not null,
	MinSearchOrder    integer not null,
	MaxSearchOrder    integer not null,
	primary key (Level0Type, Level1Type)
);

insert into @TypeOrder
select Level0Type, Level1Type, min(SearchOrder), max(SearchOrder)
from VanguardDocumentation.Level1Entities
group by Level0Type, Level1Type;

-- Same order for different types
if
	exists (
		select *
		from @TypeOrder as a
			inner join @TypeOrder as b on (a.MinSearchOrder = b.MinSearchOrder)
		where a.Level0Type <> b.Level0Type or a.Level1Type <> b.Level1Type
	)
begin
	raiserror ('Types have same SearchOrder', 16, 3);
end;

-- Different order for same type
if
	exists (
		select *
		from @TypeOrder
		where MinSearchOrder <> MaxSearchOrder
	)
begin
	raiserror ('Different order within the same type', 16, 4);
end;
GO




